package com.yoomiito.app.ui.my.remain.transfer;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.yoomiito.app.R;
import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseLazyFragmentPresenter;
import com.yoomiito.app.model.my.MyRemainMoneyInfo;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.utils.encrypt.AESUtils;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.YMToast;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;

public class TransferPresenter extends BaseLazyFragmentPresenter<TransferFragment> {
    public TransferPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void getIsAgent() {
        YmApi.getYmService()
                .getMoneyAgent()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<MyRemainMoneyInfo>getApiTransformerData())
                .compose(XApi.<MyRemainMoneyInfo>getScheduler())
                .subscribe(new ApiSubscriber<MyRemainMoneyInfo>() {
                    @Override
                    public void onSuccess(MyRemainMoneyInfo data) {
                        getV().setMoney(data);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        YMToast.showText(error.getMsg());
                    }
                });
    }

    public void transfer(String t_uid,String money,String encoded_password){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("t_uid",t_uid);
        jsonObject.put("money",money);
        String json = AESUtils.rsaEncrypt(jsonObject.toJSONString());
        String json1 = AESUtils.rsaEncrypt(encoded_password);
        if (TextUtils.isEmpty(json)||TextUtils.isEmpty(json1)){
            getV().rsaPswError();
            YMToast.showText("转账失败，请重试");
            return;
        }
        YmApi.getYmService()
                .transfer( json,json1 )
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>() {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        getV().transferSuccess();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                        if (error.getCode() == ApiException.PAY_PSW_ERROR) {
                            getV().error(true, ResUtil.getString(R.string.psw_error));
                        } else {
                            getV().error(false,"");
                            YMToast.showText(error.getMsg());
                        }
                    }
                });
    }
}
